package com.itheima.service;


import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.mapper.client.ShoppingCartMapper;
import com.itheima.pojo.client.UpdateShoppingCartByDishIdDto;
import com.itheima.pojo.client.selectShoppingCartAllVo.SelectShoppingCartAllVo;
import com.itheima.pojo.client.addShoppingCart.AddShoppingCartDto;
import com.itheima.pojo.client.addShoppingCart.AddShoppingCartVo;
import com.itheima.pojo.client.selectShoppingCartAllVo.SelectShoppingCartByDishIdVo;
import com.itheima.pojo.client.selectShoppingCartAllVo.SelectShoppingCartBySetmealIdVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 菜品列表service层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShoppingCartService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    //添加购物车
    public AddShoppingCartVo addShoppingCart(AddShoppingCartDto shoppingCartDto) {

        //设置购物车id
        long id = snowFlake.nextId();
        shoppingCartDto.setId(id + "");



        //设置redis锁
        redisTemplate.opsForValue().setIfAbsent("lock_" + shoppingCartDto.getUserId(), LocalDateTime.now().toString(), 1, TimeUnit.SECONDS);


        //判断菜品/套餐份数
        if (shoppingCartDto.getDishId() != null) {
            //如果菜品id不为空则通过菜品id与uestid查询该购物车
            SelectShoppingCartByDishIdVo dish = shoppingCartMapper.selectShoppingCartByDishId(shoppingCartDto.getDishId(), shoppingCartDto.getUserId());
            //如果查出来的dish不为空则增加一个数量
            if (dish != null) {
                //增加数量中。。
                Integer number = dish.getNumber();
                number = number + 1;
                shoppingCartDto.setNumber(number);
                //删除本条购物车数据
                shoppingCartMapper.deleteShoppingCartByDishId(dish);
                //将最新的购物车添加进去
                shoppingCartMapper.addShoppingCart(shoppingCartDto);
            } else {
                //如果查出来的dish则直接将购物车添加进去
                shoppingCartDto.setNumber(1);
                shoppingCartMapper.addShoppingCart(shoppingCartDto);
            }
        } else {
            //如果套餐id不为空则通过套餐id与uestid查询该购物车
            SelectShoppingCartBySetmealIdVo setmeal = shoppingCartMapper.selectShoppingCartBySetmealId(shoppingCartDto.getSetmealId(), shoppingCartDto.getUserId());
            //如果查出来的setmeal不为空则增加一个数量
            if (setmeal != null) {
                //增加数量中。。
                Integer number = setmeal.getNumber();
                number = number + 1;
                shoppingCartDto.setNumber(number);
                //删除本条购物车数据
                shoppingCartMapper.deleteShoppingCartBySetmealId(setmeal);
                //将最新的购物车添加进去
                shoppingCartMapper.addShoppingCart(shoppingCartDto);
            } else {
                //如果查出来的setmeal则直接将购物车添加进去
                shoppingCartDto.setNumber(1);
                shoppingCartMapper.addShoppingCart(shoppingCartDto);
            }
        }

        return shoppingCartMapper.selectShoppingCartById(shoppingCartDto);
    }

    //查询购物车列表通过userid
    public List<SelectShoppingCartAllVo> selectShoppingCartAll(String userId) {

        return shoppingCartMapper.selectShoppingCartAll(userId);
    }

    //通过userid删除购物车
    public boolean deleteShoppingCartByUserId(String userId) {
        int i = shoppingCartMapper.deleteShoppingCartByUserId(userId);
        return i > 0;
    }

    //通过菜品id/套餐id与userid修改商品份数(减一操作)
    public List<SelectShoppingCartAllVo> updateShoppingCartByDishId(UpdateShoppingCartByDishIdDto id) {

        if (id.getDishId() != null) {
            //如果dishid不为空则查询通过菜品id与userid查询购物车
            SelectShoppingCartByDishIdVo dish = shoppingCartMapper.selectShoppingCartByDishId(id.getDishId(), id.getUserId());
            //查询当前的份数
            Integer number = dish.getNumber();
            //如果份数等于0，则删除当前的购物车项并且查询购物车列表
            if (number <= 1) {
                int i = shoppingCartMapper.deleteShoppingCartByDishId(dish);
                //如果删除失败则返回一个null，删除成功则返回列表
                if (i > 0) {
                    return shoppingCartMapper.selectShoppingCartAll(id.getUserId());
                } else {
                    return null;
                }
            }
            //份数不等于0则份数减一
            dish.setNumber(number - 1);
            //修改当前份数并且查询购物车列表
            int i = shoppingCartMapper.updateShoppingCartByDishId(dish);
            //如果删除失败则返回一个null，删除成功则返回列表
            if (i > 0) {
                return shoppingCartMapper.selectShoppingCartAll(id.getUserId());
            } else {
                return null;
            }
        } else {
            //如果dishid为空则查询通过套餐id与userid查询购物车
            SelectShoppingCartBySetmealIdVo setmeal = shoppingCartMapper.selectShoppingCartBySetmealId(id.getSetmealId(), id.getUserId());
            //查询当前的份数
            Integer number = setmeal.getNumber();

            //如果份数等于0，则删除当前的购物车项并且查询购物车列表
            if (number <= 1) {
                int i = shoppingCartMapper.deleteShoppingCartBySetmealId(setmeal);
                //如果删除失败则返回一个null，删除成功则返回列表
                if (i > 0) {
                    return shoppingCartMapper.selectShoppingCartAll(id.getUserId());
                } else {
                    return null;
                }
            }
            //份数不等于0则份数减一
            setmeal.setNumber(number - 1);
            //修改当前份数并且查询购物车列表
            int i = shoppingCartMapper.updateShoppingCartBySetmealId(setmeal);
            //如果删除失败则返回一个null，删除成功则返回列表
            if (i > 0) {
                return shoppingCartMapper.selectShoppingCartAll(id.getUserId());
            } else {
                return null;
            }
        }
    }

}
